﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using FineUI;

public partial class Manager_ArticlesAdd : System.Web.UI.PageEx
{
    private string strPageCode = "P0000C";
    private string strLoginResult = string.Empty;
    private int iSelectId = 0;
    private int iSelectClassId = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        int.TryParse(Convert.ToString(Request.QueryString["ArticleId"]), out iSelectId);
        int.TryParse(Convert.ToString(Request.QueryString["Class"]), out iSelectClassId);
        if (!DalEx.DbManagerEx.CheckLoginRight(UserType, UserId, strPageCode, ref strLoginResult))
        {
            FineUI.Alert.Show(strLoginResult, FineUI.MessageBoxIcon.Warning);
            Response.Redirect("Login.aspx");
        }
        if (!Page.IsPostBack)
        {
            btnReset.OnClientClick = sfObject.GetResetReference();
            DataBinds();
        }
    }
    private void DataBinds()
    {
        if (!string.IsNullOrEmpty(Request.QueryString["Class"]))
        {
            hidClassID.Text = Request.QueryString["Class"].ToString();
        }

        dpAddTime.Text = DateTime.Now.ToString("yyyy-MM-dd");   
        List<Model.DbClass> lstClass = new List<Model.DbClass>();
        DalEx.DbClassEx.GetClassList(0, " and cType=0", ref lstClass);
        ddlClass.DataSource = lstClass;
        ddlClass.DataValueField = "CId";
        ddlClass.DataTextField = "CName";
        ddlClass.DataSimulateTreeLevelField = "CLevel";
        ddlClass.DataBind();
        ddlClass.Items.Insert(0, new FineUI.ListItem("请选择文章类别", string.Empty));

        if (iSelectId > 0)//如果产品ID>0时绑定产品属性
        {
            ddlClass.SelectedValue = iSelectClassId.ToString();
            Model.DbArticles dbArticle = DalEx.DbArticlesEx.Select(iSelectId);
            if (dbArticle != null)
            {
                hfArticleId.Text = dbArticle.AId.ToString();
                txtTitle.Text = dbArticle.ATitle;
                txtKeywords.Text = dbArticle.AKeywords;
                txtDescription.Text = dbArticle.ADescription;
                ddlClass.SelectedValue = dbArticle.AClassId.ToString();
                dpAddTime.Text = dbArticle.AAddTime.ToString("yyyy-MM-dd");
                //UEditor1.Value = HttpUtility.HtmlDecode(dbArticle.ANote);
                txtHits.Text = Convert.ToString(dbArticle.AHits);
                txtOrder.Text = Convert.ToString(dbArticle.AOrder);
                
                //List<string> lst = DalEx.DbArticlesEx.GetAllClassList(dbArticle.AClassIds);
                //foreach (FineUI.CheckItem lItem in cblClass.Items)
                //{
                //    if (lst.Contains(lItem.Value))
                //        lItem.Selected = true;
                //}

                PageContext.RegisterStartupScript(String.Format("updateUEditor1({0});", JsHelper.Enquote(HttpUtility.HtmlDecode(dbArticle.ANote))));
                PageContext.RegisterStartupScript(String.Format("updateUEditor2({0});", JsHelper.Enquote(HttpUtility.HtmlDecode(dbArticle.AShortInstr))));
            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Int16 iHits = 0, iOrder = 0, iStatus = 0, iIsPhotoNews = 0;
        int iSelectId = 0, iSelectClassId=0;

        string strFrom = SqlHelper.String(txtFrom.Text);
        string strAuthor = SqlHelper.String(txtAuthor.Text);
        //string strTitleStyle = ddlTitleStyle.SelectedValue;
        string strTitle = txtTitle.Text.Trim();
        string strHtmlTitle = string.Empty;
        string strSelectClass = ddlClass.SelectedValue ;
        string strKeywords = txtKeywords.Text.Trim();
        string strDescription = txtDescription.Text.Trim();
        string strShortInstr = Request.Form["UEditor2"].ToString();
        strShortInstr = HttpUtility.HtmlEncode(strShortInstr);
        string strHits = txtHits.Text.Trim();
        string strOrder = txtOrder.Text.Trim();
        string strSelectId = hfArticleId.Text.Trim();
        string strAddTime = dpAddTime.Text;
        string strNotes = Request.Form["UEditor1"].ToString();
        strNotes = HttpUtility.HtmlEncode(strNotes);
        Int32.TryParse(strSelectClass, out iSelectClassId);
        Int32.TryParse(strSelectId, out iSelectId);
        Int16.TryParse(strOrder, out iOrder);
        Int16.TryParse(strHits, out iHits);
        DateTime dtAddTime = DateTime.Now;
        DateTime.TryParse(strAddTime, out dtAddTime);
        iStatus = Convert.ToInt16(cboxStatus.Checked);
        iIsPhotoNews = 0;
        strHtmlTitle = strTitle;
        #region 上传图片
        string strProdImages = string.Empty;
        if (iIsPhotoNews == 1)
        {
            string strErrorMsg = string.Empty;
            strProdImages = WebBase.Comm.GetFilePath(fuProdImages, Server.MapPath("~/upload/news/"), ResolveUrl("~/upload/news/"), AllowUpFileExt, ref strErrorMsg);// GetFilePath(fuProdImages, iProdClass, AllowUpFileExt, ref strErrorMsg);
            if (string.IsNullOrEmpty(strProdImages) && iSelectId == 0)
            {
                FineUI.Alert.Show("图片新闻必须上传图片！", FineUI.MessageBoxIcon.Warning);
                return;
            }
        }
        #endregion
        Model.DbClass dbClass = DalEx.DbClassEx.Select(iSelectClassId);
        string strClassIds = string.Empty;
        if (dbClass != null)
        {
            strClassIds = dbClass.CAllClassIds + iSelectClassId + "|";
        }
        else
        {
            strClassIds = "|" + iSelectClassId + "|";
        }
        Model.DbArticles dbArticles = new Model.DbArticles();
        dbArticles.AId = iSelectId;
        dbArticles.AHtmlTitle = strHtmlTitle;
        dbArticles.ATitle = strTitle;
        dbArticles.AClassId = Convert.ToInt32(hidClassID.Text);
        dbArticles.AKeywords = strKeywords;
        dbArticles.ADescription = strDescription;
        dbArticles.AShortInstr = strShortInstr;
        dbArticles.AHits = iHits;
        dbArticles.AOrder = iOrder;
        dbArticles.AStatus = iStatus;
        dbArticles.ANote = strNotes;
        dbArticles.AAddTime = dtAddTime;
        dbArticles.AFrom = strFrom;
        dbArticles.AAuthor = strAuthor;
        dbArticles.AIsPhotoNews = iIsPhotoNews;
        dbArticles.APicture = strProdImages;
        dbArticles.AClassIds = "|" + iSelectClassId + "|"; ;
        dbArticles.CEndDate = SqlHelper.MinDate;
        dbArticles.ALang = "YuanZhang";

        string strResult = string.Empty;
        if (DalEx.DbArticlesEx.InsertEx(dbArticles, ref strResult) > 0)
        {
            sfObject.GetResetReference();
            FineUI.Alert.Show(strResult, FineUI.MessageBoxIcon.Information);
        }
        else
        {
            FineUI.Alert.Show(strResult, FineUI.MessageBoxIcon.Warning);
        }
    }
    protected void btnLoadData_Click(object sender, EventArgs e)
    {
        if (iSelectId > 0)//如果产品ID>0时绑定产品属性
        {
            Model.DbArticles dbArticle = DalEx.DbArticlesEx.Select(iSelectId);
            if (dbArticle != null)
            {
                PageContext.RegisterStartupScript(String.Format("updateUEditor1({0});", JsHelper.Enquote(HttpUtility.HtmlDecode(dbArticle.ANote))));
                PageContext.RegisterStartupScript(String.Format("updateUEditor2({0});", JsHelper.Enquote(HttpUtility.HtmlDecode(dbArticle.AShortInstr))));
            }
        }
    }
    protected void btnGetShortInfo_Click(object sender, EventArgs e)
    {
        string content = Request.Form["UEditor1"].ToString();
        Regex regex = new Regex(@"<[^>]+>|</[^>]+>");
        content = regex.Replace(content, "");
        if (content.Length > 200)
        {
            content = content.Substring(0, 197) + "...";
        }
        PageContext.RegisterStartupScript(String.Format("updateUEditor2({0});", JsHelper.Enquote(content)));
    }
}
